﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Aden.Model.Net;
using Aden.Model.SFEED;
using Aden.Util.AdenCommon;
using Aden.Util.Common;
using Aden.BLL.SFEED;

namespace AdenWebAPI.Controllers
{
    public class SFEEDController : BaseController
    {
        [HttpPost]
        public Response CheckWechatLoginUser(string token, [FromBody] Dictionary<string, string> user)
        {
            Response response = new Response();
            try
            {
                if (string.IsNullOrWhiteSpace(token) || !token.Equals(_token))
                {
                    response.code = "404";
                    response.message = "Invalid token";
                }
                else
                {
                    bool rst = SFEEDHelper.CheckWechatLoginUser(user["wechatId"]);

                    if (!rst)
                    {
                        response.code = "500";
                        response.message = "登陆失败";
                    }
                    else
                    {
                        response.code = "200";
                        response.content = SFEEDHelper.GetWechatMenuList(user["wechatId"]);
                    }
                }
            }
            catch(Exception ex)
            {
                response.code = "500";
                response.message = ex.Message;
            }
            return response;
        }

        [HttpPost]
        public Response GetTableSODatas(string token, [FromBody] OrderRequest request)
        {
            Response response = new Response();
            try
            {
                if(string.IsNullOrWhiteSpace(token) || !token.Equals(_token))
                {
                    response.code = "404";
                    response.message = "Invalid Token";
                }
                else
                {
                    var data = SFEEDHelper.GetSODatas(request.maxOrderId, request.siteGuid, request.orderDate, 
                        request.orderStatus, request.orderCode, request.language);
                    if (data == null || !data.Any())
                    {
                        response.code = "500";
                        response.message = "无效订单,请重新输入订单号";
                    }
                    else
                    {
                        response.code = "200";
                        response.content = data;
                    }
                }
            }
            catch(Exception ex)
            {
                response.code = "500";
                response.message = ex.Message;
            }
            return response;
        }

        [HttpPost]
        public Response UpdateSOStatus(string token, [FromBody] OrderRequest request)
        {
            Response response = new Response();
            try
            {
                if (string.IsNullOrWhiteSpace(token) || !token.Equals(_token))
                {
                    response.code = "404";
                    response.message = "Invalid Token";
                }
                else
                {
                    var data = SFEEDHelper.UpdateSOStatus(request.headGuid, request.orderStatus, request.user);
                    if (data == 0)
                    {
                        response.code = "500";
                        response.message = "订单状态更新失败";
                    }
                    else
                    {
                        response.code = "200";
                        response.content = "ok";
                    }
                }
            }
            catch (Exception ex)
            {
                response.code = "500";
                response.message = ex.Message;
            }
            return response;
        }
    }
}